/**
 * Copyright (c) 2011-9-23 WKM and/or its affiliates. 
 * Use is subject to license terms.
 * All rights reserved.
 */
package org.wkm.definition;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wkm.task.file.DataReader;
import org.wkm.util.Configuration;
import org.wkm.util.FileUtil;
import org.wkm.util.Home;

/**
 * BootUp
 * @version 1.0 2012-1-17
 * @author WUKUNMENG
 */
public class Bootup {
	
	private static Logger logger = LoggerFactory.getLogger(Bootup.class);
	
	//系统配置文件
	private static DataReader config = DataReader.getInstance();
	
	
	/**
	 * 默认路径下创建文件快捷方式
	 * @param path
	 */
	public void createQuickStartFile(){
		createQuickStartFile(Home.getHomePath());
	}
	
	/**
	 * 创建文件快捷方式
	 * @param path
	 */
	public void createQuickStartFile(String path){
		File filePath = new File(path);
		if(!filePath.exists()) {
			return ;
		}
		File file = new File(filePath, Configuration.TMP_CMD_FILE_NAME);
		try{
			FileWriter writer = new FileWriter(file);
			writer.write(win32CMD(filePath.getCanonicalPath()));
			writer.flush();
			writer.close();
			
			//等待文件生成完毕
			Thread.sleep(2000);
			
			//执行CMD文件
			Runtime.getRuntime().exec("cmd /k start " + file.getCanonicalPath());
			
			//等待文件执行完成
			Thread.sleep(2000);
			
			//完成后删除批处理文件
			file.deleteOnExit();
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
			logger.error(e.getMessage());
		} catch (InterruptedException e){
			// TODO: handle exception
			e.printStackTrace();
			logger.error(e.getMessage());
		}
	}
	
	/**
	 * window默认下将快捷方式放到自启动目录下
	 * @param path
	 */
	public void createAutoStart(){
		createAutoStart(Home.getHomePath());
	}
	
	/**
	 * window下将快捷方式放到自启动目录下
	 * @param path
	 */
	public void createAutoStart(String path){
		//system.auto.start.dir
		File userHome = new File(FileUtil.userHome);
		if(!userHome.exists()){
			return;
		}
		File filePath = new File(path);
		if(!filePath.exists()) {
			return ;
		}
		try{
			File start = FileUtil.scanFile(userHome, config.getProperty("system.auto.start.dir"));
			if(start == null) {
				return ;
			}
			
			File file = new File(filePath, Configuration.LINKNAME);
			File startFile = new File(start, Configuration.LINKNAME);
			if(!file.exists()){
				return ;
			}
			
			FileUtil.copyFile(file, startFile);
			
			file.deleteOnExit();
		}catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
			logger.error(e.getMessage());
		}
	}
	
	/**
	 * 32位操作系统，创建快捷方式的批命令
	 * @param filePath
	 * @return
	 */
	private String win32CMD(String filePath){
		StringBuffer buffer = new StringBuffer();
		buffer.append(filePath.substring(0, 2));
		buffer.append(FileUtil.lineSeparator);
		buffer.append("cd " + filePath);
		buffer.append(FileUtil.lineSeparator);
		buffer.append("set path=" + filePath + FileUtil.separator + "cmd.bat");
		buffer.append(FileUtil.lineSeparator);
		buffer.append("echo [InternetShortcut] >>" + Configuration.LINKNAME);
		buffer.append(FileUtil.lineSeparator);
		buffer.append("echo URL=\"%path%\" >>" + Configuration.LINKNAME);
		buffer.append(FileUtil.lineSeparator);
		buffer.append("echo IconIndex=27 >>" + Configuration.LINKNAME);
		buffer.append(FileUtil.lineSeparator);
		buffer.append("echo IconFile=%SystemRoot%\\system32\\shell32.dll >>" + Configuration.LINKNAME);
		buffer.append(FileUtil.lineSeparator);
		buffer.append("exit");
		buffer.append(FileUtil.lineSeparator);
		logger.info(FileUtil.lineSeparator + "win32-CreateQuickStart:" + FileUtil.lineSeparator + buffer.toString());
		return buffer.toString();
	}
	
	/**
	 * 是否创建自动启动文件
	 * @return
	 */
	public boolean isCreateAutoFile(){
		
		File userHome = new File(FileUtil.userHome);
		if(!userHome.exists()){
			return false;
		}
		
		try{
			File start = FileUtil.scanFile(userHome, config.getProperty("system.auto.start.dir"));
			if(start == null) {
				return false;
			}
			
			File file = new File(start, Configuration.LINKNAME);
			if(file.exists()){
				return true;
			}
			
		}catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		
		return false;
	}
	
	/**
	 * 删除自动启动文件
	 * @return
	 */
	public boolean deleteAutoFile(){
		
		File userHome = new File(FileUtil.userHome);
		if(!userHome.exists()){
			return false;
		}
		
		try{
			File start = FileUtil.scanFile(userHome, config.getProperty("system.auto.start.dir"));
			if(start == null) {
				return false;
			}
			
			File file = new File(start, Configuration.LINKNAME);
			if(file.exists()){
				return file.delete();
			}
			
		}catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		
		return false;
	}
	
	public static void main(String[] args) {
		Bootup boot = new Bootup();
		boot.createQuickStartFile("D:\\other");
		boot.createAutoStart("D:\\other");
	}
}
